//为开始时间赋予默认值
window.onload = function() {
	var date = new Date();
	//下拉框改变事件
	$("#type").change(function() {
		getProcessTemp();
	});
	var datastr = date.getFullYear() + "-" + (appendzero(date.getMonth()+ 1)) + "-"+ appendzero((date.getDate()));
	$("#planStartTime").val(datastr).trigger("change");
}
//跳转
function toIndex(){
	var url = basePath+"/bus/plan/list";
	window.location.href=url;
}
//如果月份和天数一位数则补零
function appendzero (obj) {
	if (obj < 10) return "0" + obj; else return obj
}
// 下一步 第一步
function nextOne() {
	if(getModelInfo()){
		checkRepeat();
		getProcessTemp();
	}
}
//上一步 第二步
function previousStep(){
	$("#departmentId").show();
	$("#planId").hide();
	$("#materials").hide();
	$("#clTitel").hide();
	$("#bpTitel").html("");
	$(".progress-step-one:eq(1) span").removeClass("step-line-green")
    $(".progress-step-one:eq(2) span").removeClass("step-line-green")
    $(".addInfo-one").css("display","block")
    $(".addInfo-two").css("display","none")
    $(".addInfo-three").css("display","none")
}
//下一步 第二步
function nextTwo(){
	if(getModelPlanInfo()){
		var modelCode = $("#modelCode").val();// 部品番号
		$("#modelCode3").val(modelCode);
		$("#departmentId").hide();
		$("#planId").hide();
		$("#materials").show();
		$("#clTitel").show();
		$(".progress-step-one:eq(2) span").addClass("step-line-green")
        $(".addInfo-one").css("display","none")
        $(".addInfo-two").css("display","none")
        $(".addInfo-three").css("display","block")
	}
}
//上一步 第三步
function previousStepTwo(){
	$("#departmentId").hide();
	$("#planId").show();
	$("#materials").hide();
	$("#clTitel").hide();
	$(".progress-step-one:eq(2) span").removeClass("step-line-green")
    $(".addInfo-one").css("display","none")
    $(".addInfo-two").css("display","block")
    $(".addInfo-three").css("display","none")
}

// 计划开始时间改变事件
function calculatePlanTime() {
	var planStartTime = $("#planStartTime").val();// 获取开始时间
	var trList = $("#process_model tr");
	var defaultDay = 0;
	//循环遍历表格
	for (var i=1;i<trList.length;i++) {
		var tdArrs = trList.eq(i).find("td");
		var hs = tdArrs.eq(2).find('input').val();
		 defaultDay += Number(hs); 
		 if(i==1){
			 var time = addDays(planStartTime, hs-1);
			 tdArrs.eq(4).find('span').html(planStartTime);
			 tdArrs.eq(5).find('span').html(time);
		 }else{
			 var tdArr = trList.eq(i-1).find("td");
			 var sTime = tdArr.eq(5).find('span').html();
			 var eTime = addDays(sTime,hs);
			 if(hs>0){
				 tdArrs.eq(4).find('span').html(addDays(sTime,1));
			 }else{
				 tdArrs.eq(4).find('span').html(sTime);
			 }
			 tdArrs.eq(5).find('span').html(eTime);
		 }
		
	}
	var planEndTime = addDays(planStartTime, defaultDay);// 计算计划结束时间
	$("#planEndTime").val(planEndTime);// 设置计划结束时间
	$("#totalDay").val(defaultDay);// 设置总工时
}

// 日期加上天数后的新日期.
function addDays(date, days) {
	var nd = new Date(date);
	nd = nd.valueOf();
	nd = nd + days * 24 * 60 * 60 * 1000;
	nd = new Date(nd);
	var y = nd.getFullYear();
	var m = nd.getMonth() + 1;
	var d = nd.getDate();
	if (m <= 9)
		m = "0" + m;
	if (d <= 9)
		d = "0" + d;
	var cdate = y + "-" + m + "-" + d;
	return cdate;
}

// 两个日期的差值(d1 - d2)
function DateDiff(d1, d2) {
	
	//Date.parse() 解析一个日期时间字符串，并返回1970/1/1 午夜距离该日期时间的毫秒数
	var time1 = Date.parse(new Date(d1));
	var time2 = Date.parse(new Date(d2));
	var nDays = Math.abs(parseInt((time1 - time2)/1000/3600/24));
	return  nDays;

}// end fun

var regu = /^[0-9]+\.?[0-9]*$/;
// 部品新增
function getModelInfo() {
	var modelCode = $("#modelCode").val();// 部品番号
	var modelTexture = $("#modelTexture").val();// 材质
	var modelType = $("#modelType").val();// 机种
	var modelBl = $("#modelBl").val();// 量产设备
	var modelLevel = $("#modelLevel").val();// 模具等级
	var modelPr = $("#modelPr").val();// 机床信息
	var modelPly = $("#modelPly").val();// 板厚
	/*var status = $("#status").val();*/// 状态
	var customer = $("#customer").val();// 所属客户
	//var engineering = $("#engineering").val();//工程
	var changeNo = $("#changeNo").val();//设变号
	if (modelCode == "" || modelCode == null || modelCode == "undefined") {
		msgFeedBack("部品番号不能为空","#modelCode");
		return false;
	}
	/*if (engineering == "" || engineering == null || engineering == "undefined") {
		msgFeedBack("工程不能为空","#engineering");
		return false;
	}*/
	if (modelType == "" || modelType == null || modelType == "undefined") {
		msgFeedBack("机种不能为空","#modelType");
		return false;
	}
	if (modelBl == "" || modelBl == null || modelBl == "undefined") {
		msgFeedBack("量产设备不能为空","#modelBl");
		return false;
	}
	if (modelLevel == "" || modelLevel == null || modelLevel == "undefined") {
		msgFeedBack("部品难易度不能为空","#modelLevel");
		return false;
	}
	/*if (modelPr == "" || modelPr == null || modelPr == "undefined") {
		msgFeedBack("机床信息不能为空","#modelPr");
		return false;
	}*/
	if (modelPly == "" || modelPly == null || modelPly == "undefined") {
		msgFeedBack("板厚不能为空","#modelPly");
		return false;
	}else{
		if (!regu.test(modelPly)) {
	    	  msgFeedBack("请输入正确的数字","#modelPly");
	    	  return false;
	      } else {
	        if (modelPly.indexOf('.') > -1) {
	          if (modelPly.split('.')[1].length > 2) {
	        	  msgFeedBack("小数点后面保留2位","#modelPly");
	        	  return false;
	          }
	        }
	      }
	}
	/*if (status == "" || status == null || status == "undefined") {
		msgFeedBack("请选择状态","#status");
		return false;
	}*/
	if (customer == "" || customer == null || customer == "undefined") {
		msgFeedBack("所属客户不能为空","#customer");
		return false;
	}
	if (modelTexture == "" || modelTexture == null || modelTexture == "undefined") {
		msgFeedBack("材质不能为空","#modelTexture");
		return false;
	}
	/*if (changeNo == "" || changeNo == null || changeNo == "undefined") {
		msgFeedBack("设变号不能为空","#changeNo");
		return false;
	}*/
	return true;
}
function checkRepeat(){
	var modelCode = $("#modelCode").val();// 部品番号
	//var engineering = $("#engineering").val();//工程
	var bpresult = [];
	var bpdata = {
		modelCode : modelCode
	};
	bpresult.push(bpdata);
	var url =basePath + "/bus/plan/checkRepeat"
		$.ajax({
			type : "post",
			url : url,
			data : {bpdata:JSON.stringify(bpresult)},
			dataType : "json",
			success : function(data) {
				if(data.data){
					layer.msg(data.message, {
						  offset: 'auto',
						  anim: 1
					});
				}else{
					var modelCode = $("#modelCode").val();// 部品番号
					$("#modelCode2").val(modelCode);
					$("#departmentId").hide();
					$("#planId").show();
					$("#bpTitel").html("<span id='arrows-left'>> 计划信息</span>");
					
					$(".progress-step-one:eq(1) span").addClass("step-line-green")
			        $(".addInfo-one").css("display","none")
			        $(".addInfo-two").css("display","block")
			        $(".addInfo-three").css("display","none")
				}
			}
		});
}



//获取模具计划信息
function getModelPlanInfo(){
	var modelCode = $("#modelCode2").val();// 部品番号
	var factory = $("#factory").val();//厂商
	var planStartTime = $("#planStartTime").val();//计划开始时间
	var planEndTime = $("#planEndTime").val();//计划结束时间
	var planTotalTime = $("#totalDay").val();//总耗时
	if (factory == "" || factory == null || factory == "undefined") {
		msgFeedBack("请选择厂家","#factory");
		return false;
	}
	if (planStartTime == "" || planStartTime == null || planStartTime == "undefined") {
		msgFeedBack("计划开始时间不能为空","#planStartTime");
		return false;
	}
	return true;
}

//获取table每一行数据
function getTableValue(){
	//var bpresult = getModelInfo();//获取部品信息
	var modelCode = $("#modelCode").val();// 部品番号
	var modelTexture = $("#modelTexture").val();// 材质
	var modelType = $("#modelType").val();// 机种
	var modelBl = $("#modelBl").val();// 量产设备
	var modelLevel = $("#modelLevel").val();// 模具等级
	var modelPr = $("#modelPr").val();// 机床信息
	var modelPly = $("#modelPly").val();// 板厚
	var status = $("#status").val();// 状态
	var customer = $("#customer").val();// 所属客户
	var engineering = "";//工程
	var changeNo = $("#changeNo").val();//设变号
	/*if (modelCode == "" || modelCode == null || modelCode == "undefined") {
		msgFeedBack("模具番号不能为空","#modelCode");
		return false;
	}
	if (modelTexture == "" || modelTexture == null || modelTexture == "undefined") {
		msgFeedBack("材质不能为空","#modelTexture");
		return false;
	}
	if (modelType == "" || modelType == null || modelType == "undefined") {
		msgFeedBack("机种不能为空","#modelType");
		return false;
	}
	if (modelBl == "" || modelBl == null || modelBl == "undefined") {
		msgFeedBack("量产设备不能为空","#modelBl");
		return false;
	}
	if (modelLevel == "" || modelLevel == null || modelLevel == "undefined") {
		msgFeedBack("模具等级不能为空","#modelLevel");
		return false;
	}
	if (modelPr == "" || modelPr == null || modelPr == "undefined") {
		msgFeedBack("机床信息不能为空","#modelPr");
		return false;
	}
	if (modelPly == "" || modelPly == null || modelPly == "undefined") {
		msgFeedBack("板厚不能为空","#modelPly");
		return false;
	}
	if (status == "" || status == null || status == "undefined") {
		msgFeedBack("请选择状态","#status");
		return false;
	}
	if (customer == "" || customer == null || customer == "undefined") {
		msgFeedBack("所属客户不能为空","#customer");
		return false;
	}
	if (engineering == "" || engineering == null || engineering == "undefined") {
		msgFeedBack("工程不能为空","#engineering");
		return false;
	}
	if (changeNo == "" || changeNo == null || changeNo == "undefined") {
		msgFeedBack("设变号不能为空","#changeNo");
		return false;
	}*/
	var bpresult = [];
	var bpdata = {
		modelCode : modelCode,
		modelTexture : modelTexture,
		modelType : modelType,
		modelBl : modelBl,
		modelLevel : modelLevel,
		modelPr : modelPr,
		modelPly : modelPly,
		status : "1",
		customer : customer,
		engineering : engineering,
		changeNo : changeNo
	};
	bpresult.push(bpdata);

	//planresult = getModelPlanInfo();//获取模具计划信息
	var modelCode = $("#modelCode2").val();// 部品番号
	var factory = $("#factory").val();//厂商
	var planStartTime = $("#planStartTime").val();//计划开始时间
	var planEndTime = $("#planEndTime").val();//计划结束时间
	var planTotalTime = $("#totalDay").val();//总耗时
	var type = $("#type").val();//类别
	/*if (factory == "" || factory == null || factory == "undefined") {
		msgFeedBack("请选择厂商","#factory");
		return false;
	}
	if (planStartTime == "" || planStartTime == null || planStartTime == "undefined") {
		msgFeedBack("计划开始时间不能为空","#planStartTime");
		return false;
	}*/
	var mjresult = [];
	var mjdata = {
		modelCode : modelCode,
		factory : factory,
		planStartTime : planStartTime,
		planEndTime : planEndTime,
		planTotalTime : planTotalTime,
		type : type
	};
	mjresult.push(mjdata);
	//获取工序信息
	var trList = $("#process_model tr");
	var result = [];
	var modelCode = $("#modelCode2").val();
	for (var i=1;i<trList.length;i++) {
		var tdArr = trList.eq(i).find("td");
		var processName = tdArr.eq(0).html();//工序名称
		var refTime = tdArr.eq(1).html();//参考耗时
		var planTotalTime = tdArr.eq(2).find('input').val();//预计耗时
		var planStartTime = tdArr.eq(4).find('span').html();//工序开始时间
		var planEndTime = tdArr.eq(5).find('span').html();// 工序结束时间
		var	targetPre = tdArr.eq(6).find('input').val();//精度
		var operation = tdArr.eq(7).find('span').html();//操作
		if(operation == '已完成'){
			operation = 1;
		}else{
			operation = 0;
		}
		result.push({'modelCode':modelCode,'processName':processName,'planTotalTime':planTotalTime,'planStartTime':planStartTime,'planEndTime':planEndTime,'targetPre':targetPre,'refTime':refTime,'operation':operation});
	}
	//alert(JSON.stringify(result));
	//获取计划信息
	var meterTrList = $("#meterTabInfoList tr");
	var jhresult = [];
	if(meterTrList.length>1)
	for (var i=1;i<meterTrList.length;i++) {
		var tdArr = meterTrList.eq(i).find("td");
		var modelCode3 = tdArr.eq(0).html();// 部品番号
		var useFor = tdArr.eq(1).html();//参考耗时
		var grow = tdArr.eq(2).html();
		var wide = tdArr.eq(3).html();
		var tall = tdArr.eq(4).html();
		var planNum = tdArr.eq(5).html();
		var perNum = tdArr.eq(6).html();
		var planAmount = tdArr.eq(7).html();
		var giveTime = tdArr.eq(8).html();
		var operator = tdArr.eq(9).html();//工序开始时间
		jhresult.push({'modelCode':modelCode3,'useFor':useFor,'grow':grow,'wide':wide,'tall':tall,'planNum':planNum,'perNum':perNum,'planAmount':planAmount,'giveTime':giveTime,'operator':operator});
	}else{
		if(checkMeterInput()){
			var modelCode3 = $("#modelCode3").val();// 部品番号
			var useFor = $("#useFor").val();// 用途
			var grow = $("#grow").val();// 长
			var wide = $("#wide").val();// 幅
			var tall = $("#tall").val();// 板厚
			var planNum = $("#planNum").val();// 计划枚数
			var perNum = $("#perNum").val();// 每枚可取数
			var planAmount = $("#planAmount").val();//可取台数
			var giveTime = $("#giveTime").val();// 下发时间
			var operator = $("#operator").val();// 下发人
			var jhresult = [];
			var jhdata = {
					modelCode : modelCode3,
					useFor : useFor,
					grow : grow,
					wide : wide,
					tall : tall,
					planNum : planNum,
					perNum : perNum,
					planAmount : planAmount,
					giveTime : giveTime,
					operator : operator
				};
			jhresult.push(jhdata);
		}
	}
	var url = basePath+"/bus/plan/saveBusModel"
	showLoad();
		$.ajax({
			type : "post",
			url : url,
			data : {bpdata:JSON.stringify(bpresult),mjdata:JSON.stringify(mjresult),arr:JSON.stringify(result),jhdata:JSON.stringify(jhresult)},
			dataType : "json",
			success : function(data) {
				msgPrompt(data.message);
			}
		});
}

function continueToAddMore(){
	//var useFor = $("#useFor").val("");// 用途
	$("#grow").val("");// 长
	$("#wide").val("");// 幅
	$("#tall").val("");// 板厚
	$("#planNum").val("");// 计划枚数
	$("#perNum").val("");// 每枚可取数
	$("#planAmount").val("");//可取台数
	$("#giveTime").val("");// 下发时间
	$("#operator").val("");// 下发人
}

function continueToAdd(){
	var modelCode3 = $("#modelCode3").val();// 部品番号
	var useFor = $("#useFor").val();// 用途
	var grow = $("#grow").val();// 长
	var wide = $("#wide").val();// 幅
	var tall = $("#tall").val();// 板厚
	var planNum = $("#planNum").val();// 计划枚数
	var perNum = $("#perNum").val();// 每枚可取数
	var planAmount = $("#planAmount").val();//可取台数
	var giveTime = $("#giveTime").val();// 下发时间
	var operator = $("#operator").val();// 下发人
	if(modelCode == null || modelCode == undefined || modelCode == ""){
		msgFeedBack("模具番号不能为空","#modelCod3");
		return false;
	}
	if(useFor == null || useFor == undefined || useFor == ""){
		msgFeedBack("用途不能为空","#useFor");
		return false;
	}
	if(grow == null || grow == undefined || grow == ""){
		msgFeedBack("长不能为空","#grow");
		return false;
	}
	if(wide == null || wide == undefined || wide == ""){
		msgFeedBack("幅不能为空","#wide");
		return false;
	}
	if(tall == null || tall == undefined || tall == ""){
		msgFeedBack("板厚不能为空","#tall");
		return false;
	}
	if(planNum == null || planNum == undefined || planNum == ""){
		msgFeedBack("计划枚数不能为空","#planNum");
		return false;
	}
	if(perNum == null || perNum == undefined || perNum == ""){
		msgFeedBack("每枚可取数不能为空","#perNum");
		return false;
	}
	if(planAmount == null || planAmount == undefined || planAmount == ""){
		msgFeedBack("可取台数不能为空","#planAmount");
		return false;
	}
	if(giveTime == null || giveTime == undefined || giveTime == ""){
		msgFeedBack("下发时间不能为空","#giveTime");
		return false;
	}
	if(operator == null || operator == undefined || operator == ""){
		msgFeedBack("下发人不能为空","#operator");
		return false;
	}
	$("#meterInfoList").show();
	$("#meterTabInfoList").append("<tr bordercolor='#FF0000'><td>"+modelCode3+"</td><td>"+useFor+"</td><td>"+grow+"</td><td>"+wide+"</td><td>"+tall+"</td><td>"+planNum+"</td><td>"+perNum+"</td><td>"+planAmount+"</td><td>"+giveTime+"</td><td>"+operator+"</td><td><span style = 'cursor:pointer;' onClick = 'deleteRow(this)'>删除</span></td></tr>");
}

function checkMeterInput(){
	var modelCode3 = $("#modelCode3").val();// 部品番号
	var useFor = $("#useFor").val();// 用途
	var grow = $("#grow").val();// 长
	var wide = $("#wide").val();// 幅
	var tall = $("#tall").val();// 板厚
	var planNum = $("#planNum").val();// 计划枚数
	var perNum = $("#perNum").val();// 每枚可取数
	var planAmount = $("#planAmount").val();//可取台数
	var giveTime = $("#giveTime").val();// 下发时间
	var operator = $("#operator").val();// 下发人
	if(modelCode == null || modelCode == undefined || modelCode == ""){
		return false;
	}
	if(useFor == null || useFor == undefined || useFor == ""){
		return false;
	}
	if(grow == null || grow == undefined || grow == ""){
		return false;
	}
	if(wide == null || wide == undefined || wide == ""){
		return false;
	}
	if(tall == null || tall == undefined || tall == ""){
		return false;
	}
	if(planNum == null || planNum == undefined || planNum == ""){
		return false;
	}
	if(perNum == null || perNum == undefined || perNum == ""){
		return false;
	}
	if(planAmount == null || planAmount == undefined || planAmount == ""){
		return false;
	}
	if(giveTime == null || giveTime == undefined || giveTime == ""){
		return false;
	}
	if(operator == null || operator == undefined || operator == ""){
		return false;
	}
}


//删除当前行

function deleteRow(nowTr){

  //多一个parent就代表向前一个标签,

  // 本删除范围为<td><tr>两个标签,即向前两个parent

  //如果多一个parent就会删除整个table

  $(nowTr).parent().parent().remove();

}

function complete(j){
	layer.confirm('确定要更改成完成吗?', {icon: 3, title:'提示'}, function(index){
		var trList = $("#process_model tr");
		var tdArr = trList.eq(j).find("td");
		for (var i=1;i<trList.length;i++) {
			var tdArrs = trList.eq(i).find("td");
			var obj = tdArrs.eq(7).find('span').html();
			if(obj == '完成' && i<j){
				layer.alert("此工序前有未完成的工序");
				layer.close(index);
				return false;
			}
		}
		tdArr.eq(7).html('<span>已完成</span>');//操作
		layer.close(index);
	});
	
}
//获取工序模板
function getProcessTemp(){
	var modelLevel = $("#modelLevel").val();// 部品番号
	var type = $("#type").val();//类型
	var bpdata = {
			modelLevel : modelLevel,
			type : type
	};
	var url =basePath+ "/bus/plan/getProcessTemp"
		$.ajax({
			type : "post",
			url : url,
			data : bpdata,
			dataType : "json",
			success : function(data) {
				//清空模板
				$("#processText").html("");
				//初始化工序模板
				$.each(data.data, function(index, value) {
					var str = "";
					str = "<tr><td>"+value.processName+"</td><td>"+value.referenceDate+"</td>" +
							"<td><input type = 'text' value = "+value.refTime+" id = 'hs"+index+"'/></td>" +
									"<td>"+value.unit+"</td><td><span id = 'startTime"+index+"'></span></td>" +
											"<td><span id = 'endTime"+index+"'></span></td>" +
													"<td><input type = 'text' value = '"+value.targetPre+"' id = '' /></td><td></td></tr>";
					$("#processText").append(str);
			    });
				//文本框改变事件
				$("#processText input").change(function() {
					calculatePlanTime();
				});
				//初始化工序完成结束日期
				calculatePlanTime();
			}
		});
}